<?php

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}



if ($action == 'index')
{
	$link = array(
			array(
					'name'=>'添加商品备案信息',
					'link'=>'wtd.php?act=add_product'
			),
			array(
					'name'=>'查询商品备案状态信息',
					'link'=>'wtd.php?act=query_product'
			),
			array(
					'name'=>'批量添加商品备案信息',
					'link'=>'wtd.php?act=add_product_batch'
			)
			
	);
	
	echo '<b>备案管理</b><br/>';
	foreach ($link as $k=>$v)
	{
		echo '<a href="'.$v['link'].'">'.$v['name'].'</a><br/>';
	}
	echo '<br/>';
	
	
	$link1 = array(
			array(
					'name'=>'添加入库单',
					'link'=>'wtd.php?act=add_inventory'
			),
			array(
					'name'=>'入库单查询',
					'link'=>'wtd.php?act=query_inventory'
			),
			array(
					'name'=>'商品入库查询',
					'link'=>'wtd.php?act=query_inwarehouse'
			),
			array(
					'name'=>'商品出库查询',
					'link'=>'wtd.php?act=query_outwarehouse'
			),
			array(
					'name'=>'库存查询',
					'link'=>'wtd.php?act=query_stockage'
			),
			array(
					'name'=>'批量查询库存',
					'link'=>'wtd.php?act=query_stockage_batch'
			)
				
	);
	echo '<b>库存管理</b><br/>';
	foreach ($link1 as $k=>$v)
	{
		echo '<a href="'.$v['link'].'">'.$v['name'].'</a><br/>';
	}
	echo '<br/>';
	
	
	$link2 = array(
			array(
					'name'=>'添加订单',
					'link'=>'wtd.php?act=add_order'
			),
			array(
					'name'=>'批量添加订单',
					'link'=>'wtd.php?act=add_order_batch'
			),
			array(
					'name'=>'添加支付信息',
					'link'=>'wtd.php?act=add_payinfo'
			),
			array(
					'name'=>'批量添加支付信息',
					'link'=>'wtd.php?act=add_payinfo_batch'
			),
			array(
					'name'=>'查询订单信息',
					'link'=>'wtd.php?act=query_order'
			),
			array(
					'name'=>'查询订单运单号信息',
					'link'=>'wtd.php?act=query_orderexpressno'
			),
			array(
					'name'=>'查询订单路由信息',
					'link'=>'wtd.php?act=query_express'
			)
	
	);
	echo '<b>订单管理</b><br/>';
	foreach ($link2 as $k=>$v)
	{
		echo '<a href="'.$v['link'].'">'.$v['name'].'</a><br/>';
	}
}

elseif ($action == 'add_inventory')//3.3.1	添加入库单
{
	$fields = array(
				'*入库单号'=>'ladingNo',
				'*航班号'=>'flightNo',
				'*始发地'=>'origin',
				'*口岸编码'=>'portCode',
				'进出口标志'=>'ieFlag',
				'*收货仓库'=>'whCode',
				'*货物总数'=>'total',
				'到岸日期'=>'arrivalDate',
				'商家'=>'seller',
			);
	$goods = array(
			'*商品编码'=>'productId',
			'*商品名称'=>'productName',
			'*预计到货数量'=>'applyQty',
			'*净重'=>'netWt',
			'*毛重'=>'grossWt',
			'*币制'=>'currency',
			'*单价'=>'price',
			'*总价'=>'total',
			'商品体积'=>'volume',
			'体积单位'=>'volumeUnit',
			'商品长'=>'length',
			'商品宽'=>'width',
			'商品高'=>'height',
			'长度单位'=>'linearUnit',
			'备注'=>'notes'
			
	);

	echo '添加入库单<br/><br/>';
	echo '<form action="wtd.php?act=act_add_inventory" method="post" >';
	foreach ($fields as $k=>$v)
	{
		echo $k.':<input value="" type="text" name="'.$v.'"/><br/>';
	}
	$total = 1;
	echo '<br/>货物列表<br/>';
	for ($i=0;$i<$total;$i++)
	{
		foreach ($goods as $k=>$v)
		{
			echo $k.':<input value="" type="text" name="goods['.$i.']['.$v.']"/><br/>';
		}
		echo '<br/>';
	}
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
	
	
}

elseif ($action == 'query_inventory')//3.3.2	入库单查询
{
	$fields = array(
				'*入库单号'=>'ladingNo',
			);
	echo '<b>入库单查询</b><br/><br/>';
	echo '<form action="wtd.php?act=act_query_inventory" method="post" >';
	foreach ($fields as $k=>$v)
	{
		echo $k.':<input value="" type="text" name="'.$v.'"/><br/>';
	}
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
}

elseif ($action == 'query_inwarehouse')//3.3.3	商品入库查询
{
	$fields = array(
			'*商品编码'=>'productId',
			'*开始时间'=>'startTime',
			'*结束时间'=>'endTime'
	);
	
	echo '<b>商品入库查询</b><br/><br/>';
	echo '<form action="wtd.php?act=act_query_inwarehouse" method="post" >';
	foreach ($fields as $k=>$v)
	{
		echo $k.':<input value="" type="text" name="'.$v.'"/><br/>';
	}
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
}


elseif ($action == 'query_outwarehouse')//3.3.4	商品出库查询
{
	$fields = array(
			'*商品编码'=>'productId',
			'*开始时间'=>'startTime',
			'*结束时间'=>'endTime'
	);

	echo '<b>商品出库查询</b><br/><br/>';
	echo '<form action="wtd.php?act=act_query_outwarehouse" method="post" >';
	foreach ($fields as $k=>$v)
	{
		echo $k.':<input value="" type="text" name="'.$v.'"/><br/>';
	}
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
}

elseif ($action == 'query_stockage')//3.3.5	库存查询
{
	$fields = array(
			'*商品编码'=>'productId',
	);

	echo '<b>库存查询</b><br/><br/>';
	echo '<form action="wtd.php?act=act_query_stockage" method="post" >';
	foreach ($fields as $k=>$v)
	{
		echo $k.':<input value="" type="text" name="'.$v.'"/><br/>';
	}
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
}

elseif ($action == 'query_stockage_batch')//3.3.6	批量查询库存
{
	$fields = array(
			'*仓库编码'=>'whcode',
			'商家'=>'seller',
			'页数'=>'pageSize',
			'页码'=>'pageNo',
			'排序'=>'orderby'
	);
	
	echo '<b>批量查询库存</b><br/><br/>';
	echo '<form action="wtd.php?act=act_query_stockage_batch" method="post" >';
	foreach ($fields as $k=>$v)
	{
		echo $k.':<input value="" type="text" name="'.$v.'"/><br/>';
	}
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
}

elseif ($action == 'add_order')//3.2.1	添加订单
{
	$fields = array(
			'*订单号'=>'orderId',
			'*订单日期'=>'orderDate',
			'*顾客姓名'=>'name',
			'*送货省'=>'province',
			'*送货市'=>'city',
			'*送货区/县'=>'area',
			'*送货地址'=>'addr',
			'邮政编码'=>'zipCode',
			'*证件类型'=>'cardType',
			'*顾客身份证'=>'cardNo',
			'*联系电话1'=>'phone',
			'联系电话2'=>'mobile',
			'发票抬头'=>'invoiceHead',
			'发票类型'=>'invoiceType',
			'发票内容'=>'invoice',
			'*订单总金额'=>'total',
			'*运费'=>'freight',
			'*保价费用'=>'insuredFee',
			'商家'=>'seller',
			'备注'=>'notes',
			'*口岸编码'=>'portCode',
			'进出口标志'=>'ieFlag',
			'*所属业务模式'=>'busiMode',
			'捡货类型'=>'pickMode',
			'*发货仓库代号'=>'whCode',
			'*物流公司编码'=>'expressCode',
			'运单号'=>'expressNo',
			'目的城市'=>'destCity',
			'正品不良品标记'=>'prodFlag',
			
	);
	$orderItems = array(
			'*商品编码'=>'productId',
			'*商品名称'=>'productName',
			'*商品数量'=>'qty',
			'*商品单价'=>'price',
			'*付款金额'=>'total',
			'是否组合商品'=>'isGroup',
			'组合商品条形码'=>'groupBarcode'
			
	);

	echo '<b>添加订单</b><br/>';
	echo '<form action="wtd.php?act=act_add_order" method="post" >';
	foreach ($fields as $k=>$v)
	{
		echo $v.':<input value="" type="text" name="'.$v.'"/><br/>';
	}
	echo '<br/><b>*订单明细</b><br/>';
	$total = 1;
	for ($i=0;$i<$total;$i++)
	{
		foreach ($orderItems as $k=>$v)
		{
			echo $k.':<input value="" type="text" name="orderItemList[orderItems]['.$i.']['.$v.']"/><br/>';
		}
		echo '<br/>';
	}
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
	
	
}

elseif ($action == 'add_order_batch')//3.2.2	批量添加订单
{
	$fields = array(
			'*订单号'=>'orderId',
			'*订单日期'=>'orderDate',
			'*顾客姓名'=>'name',
			'*送货省'=>'province',
			'*送货市'=>'city',
			'*送货区/县'=>'area',
			'*送货地址'=>'addr',
			'邮政编码'=>'zipCode',
			'*证件类型'=>'cardType',
			'*顾客身份证'=>'cardNo',
			'*联系电话1'=>'phone',
			'联系电话2'=>'mobile',
			'发票抬头'=>'invoiceHead',
			'发票类型'=>'invoiceType',
			'发票内容'=>'invoice',
			'*订单总金额'=>'total',
			'*运费'=>'freight',
			'*保价费用'=>'insuredFee',
			'商家'=>'seller',
			'备注'=>'notes',
			'*口岸编码'=>'portCode',
			'进出口标志'=>'ieFlag',
			'*所属业务模式'=>'busiMode',
			'捡货类型'=>'pickMode',
			'*发货仓库代号'=>'whCode',
			'*物流公司编码'=>'expressCode',
			'运单号'=>'expressNo',
			'目的城市'=>'destCity',
			'正品不良品标记'=>'prodFlag',
				
	);
	$orderItems = array(
			'*商品编码'=>'productId',
			'*商品名称'=>'productName',
			'*商品数量'=>'qty',
			'*商品单价'=>'price',
			'*付款金额'=>'total',
			'是否组合商品'=>'isGroup',
			'组合商品条形码'=>'groupBarcode'
		
	);

	echo '<b>批量添加订单</b><br/>';
	echo '<form action="wtd.php?act=act_add_order_batch" method="post" >';
	$total = 3;
	
	for ($j=0;$j<$total;$j++)
	{
		echo '订单'.($j+1).'<br/>';
		foreach ($fields as $k=>$v)
		{
			echo $v.':<input value="" type="text" name="orders['.$j.']['.$v.']"/><br/>';
		}
		echo '<br/><b>*订单明细</b><br/>';
		
		for ($i=0;$i<$total;$i++)
		{
			foreach ($orderItems as $k=>$v)
			{
				echo $k.':<input value="" type="text" name="orders['.$j.'][orderItemList][orderItems]['.$i.']['.$v.']"/><br/>';
			}
			echo '<br/>';
		}
		
	}
	
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';


}

elseif ($action == 'add_payinfo')//3.2.3	添加支付信息
{
	$fields = array(
			'payNo'=>'*支付交易号',
			'payCompname'=>'*支付企业名称',
			'payCompcode'=>'*支付企业代号',
			'payTime'=>'付款时间',
			'orderId'=>'*支付对应订单号',
			'busiMode'=>'*业务模式',
			'customscode'=>'*口岸编码',
			'webSite'=>'电商平台域名',
			'regNo'=>'电商商户备案号',
			'regName'=>'电商商户名称',
			'payAmount'=>'支付金额',
			'payCurrency'=>'支付币制代码',
			'rmbAmount'=>'人民币金额'
			
	);
	
	echo '<b>添加支付信息</b><br/>';
	echo '<form action="wtd.php?act=act_add_payinfo" method="post" >';
	foreach ($fields as $k=>$v)
	{
		echo $k.':<input value="" type="text" name="'.$k.'"/><br/>';
	}
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
}

elseif ($action == 'add_payinfo_batch')//3.2.4	批量添加支付信息
{
	$fields = array(
			'payNo'=>'*支付交易号',
			'payCompname'=>'*支付企业名称',
			'payCompcode'=>'*支付企业代号',
			'payTime'=>'付款时间',
			'orderId'=>'*支付对应订单号',
			'busiMode'=>'*业务模式',
			'customscode'=>'*口岸编码',
			'webSite'=>'电商平台域名',
			'regNo'=>'电商商户备案号',
			'regName'=>'电商商户名称',
			'payAmount'=>'支付金额',
			'payCurrency'=>'支付币制代码',
			'rmbAmount'=>'人民币金额'
		
	);

	echo '<b>批量添加支付信息</b><br/><br/>';
	echo '<form action="wtd.php?act=act_add_payinfo_batch" method="post" >';
	$total = 3;
	for ($i=0;$i<$total;$i++)
	{
		echo '表单'.($i+1).'<br/>';
		foreach ($fields as $k=>$v)
		{
			echo $k.':<input value="" type="text" name="payInfos['.$i.']['.$k.']"/><br/>';
		}
		echo '<br/>';
	}
	
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
}

elseif ($action == 'query_order')//3.2.5	查询订单信息
{
	$fields = array(
				'*订单号'=>'orderId',
				'返回字段'=>'fields'
	);
	
	echo '查询订单信息<br/>';
	echo '<form action="wtd.php?act=act_query_order" method="post" >';
	foreach ($fields as $k=>$v)
	{
		echo $k.':<input value="" type="text" name="'.$v.'"/><br/>';
	}
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
}

elseif ($action == 'query_orderexpressno')//3.2.6	查询订单运单号信息
{
	$fields = array(
			'*订单号'=>'orderId',
	);

	echo '查询订单信息<br/>';
	echo '<form action="wtd.php?act=act_query_orderexpressno" method="post" >';
	foreach ($fields as $k=>$v)
	{
		echo $k.':<input value="" type="text" name="'.$v.'"/><br/>';
	}
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
}

elseif ($action == 'query_express')//3.2.7	查询订单路由信息
{
	$fields = array(
			'订单号'=>'orderId',
			'物流单号'=>'expressNo',
			'状态'=>'state'
				
	);

	echo '查询订单信息<br/>';
	echo '<form action="wtd.php?act=act_query_express" method="post" >';
	foreach ($fields as $k=>$v)
	{
		echo $k.':<input value="" type="text" name="'.$v.'"/><br/>';
	}
	echo '<input type="submit" value="submit" /><br/>';
	echo '</form>';
}


function add_inventory($data=array())//添加入库单
{
	$method = "wtdex.stock.inventory.add";
	$data = $_POST;
	unset($data['goods']);
	$data['goodsList']['goods'] = $_POST['goods'];
	// 	print_r($data);
	$content = wtd_get_content($method,$data);
	return $content;
}
function query_inwarehouse($data=array())//3.3.3	商品入库查询
{
	$method = "wtdex.stock.inwarehouse.get";
	$data = $_POST;

	$content = wtd_get_content($method,$data);
	print_r($content);
	if ($content['code']==0)
	{

	}
	else
	{

	}
}
function query_inventory($data)//3.3.2	入库单查询
{
	$method = "wtdex.stock.inventory.get";
	$data = $_POST;

	$content = wtd_get_content($method,$data);
	print_r($content);
}

function query_outwarehouse($data=array())//3.3.4	商品出库查询
{
	$method = "wtdex.stock.outwarehouse.get";
	$data = $_POST;

	$content = wtd_get_content($method,$data);
	print_r($content);
	if ($content['code']==0)
	{

	}
	else
	{

	}
}
/**
 *
 * 查询库存
 * @param $data = array(
 * 						productId
 					)
 *
 *
 *
 *
 *  */
function query_stockage($data=array())//3.3.5	库存查询
{
	$method = "wtdex.stock.stockage.get";
// 	print_r($data);
	$content = wtd_get_content($method,$data);
// 	print_r($content);
	return $content;
}
/** 
 * 
 * 批量查询库存
 * @param $data = array(
 * 						whcode
						seller
						pageSize
						pageNo
						orderby
 								)
 *  
 *  
 *  
 *  
 *  */
function query_stockage_batch($data)//3.3.6	批量查询库存
{
	$method = "wtdex.stock.stockage.find";

	$content = wtd_get_content($method,$data);
	
	if ($content['code']==0)
	{
		return false;
	}
	else
	{
		$return['total'] = $content['total'];
		$return['stockages'] = $content['stockageList']['stockages'];
		return $return;
	}
}

function add_order($data=array())//3.2.1	添加订单
{
	$method = "wtdex.trade.order.add";
	$content = wtd_get_content($method,$data);
	return $content;
}

function add_order_batch($data=array())//3.2.2	批量添加订单
{
	$method = "wtdex.trade.order.batch";
// 	$data = $_POST;
	// 	unset($data['orderItems']);
	// 	$data['orderItemList']['orderItems']=$_POST['orderItems'];
	$content = wtd_get_content($method,$data);
	return $content;
	
}

function add_payinfo($data=array())//3.2.3	添加支付信息
{
	$method = "wtdex.trade.payinfo.add";
	$data = $_POST;
	// 	print_r($data);
	$content = wtd_get_content($method,$data);
	print_r($content);
}

function add_payinfo_batch($data=array())//3.2.4	批量添加支付信息
{
	$method = "wtdex.trade.payinfo.batch";
	$data = $_POST;
	// 	print_r($data);
	$content = wtd_get_content($method,$data);
	print_r($content);
}

function act_query_order($data=array())//3.2.5	查询订单信息
{
	$method = "wtdex.trade.order.get";

	$content = wtd_get_content($method,$data);
	return $content;
}

function query_orderexpressno($data=array())//3.2.6	查询订单运单号信息
{
	$method = "wtdex.trade.orderexpressno.get";
	$data = $_POST;

	$content = wtd_get_content($method,$data);
	print_r($content);
}

function query_express($data=array())//3.2.7	查询订单路由信息
{
	$method = "wtdex.trade.express.get";

	$content = wtd_get_content($method,$data);
	return $content;
}
function wtd_get_sign($method,$params)
{
	$format = 'json';
	$v ='1.0';
// 	$appkey = '955c2d2c-b606-48b0-be08-d76ed965fc2d';
// 	$secret = 'ef9c1a0a-ffb5-4112-8bec-c51f3b9beaaf';
// 	$token =  '45f4f1dc-e5a6-41c0-b715-fbf8889b3495';
	$appkey = '619030a8-6443-4dd8-9c04-2ec525c6d3a2';
	$secret = '716fe508-c6e2-411b-8a3d-b763fa76cb05';
	$token =  '7a9e4a8c-7c13-4853-ab1a-66d3c5f1cae7';
	$timestamp = date("Y-m-d H:i:s", time()+8*60*60);
	$sign = base64_encode(md5($secret.$appkey.$format.$method.$params.$timestamp.$token.$v.$secret));
	$result = 'appkey='.$appkey."&sign=".$sign."&token=".$token."&format=".$format."&timestamp=".$timestamp."&method=".$method."&v=".$v."&params=".$params;
	return $result;
}

function wtd_get_content( $method , $param = array() )
{

// 	$url = "http://wtd.nat123.net/wtdex-service/ws/openapi/rest/route";
	$url = "http://120.26.115.70:6060/wtdex-service/ws/openapi/rest/route";
	$params = json_encode($param);
// 	echo $params;
	$header = array("Accept-Charset: utf-8","Content-type: application/x-www-form-urlencoded");
	$data = wtd_get_sign($method, $params);
// 	echo $data.'<br/>';
	file_put_contents(ROOT_PATH.'log/wspush.txt', '【'.date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']).'——'.$method.'】'.$data.PHP_EOL, FILE_APPEND | LOCK_EX);
	$ch = curl_init();//初始化，创建句柄
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_URL, $url);//设置细节参数
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置头信息的地方
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch , CURLOPT_RETURNTRANSFER , 1);
	$str = curl_exec($ch);//获取内容
	curl_close($ch);
	$content =  json_decode($str,true);
// 	echo $str;
// 	print_r($content);
	$result = $content['response'];
	return $result;
}


